#include "jsonfileparser.h"

JsonFileParser::JsonFileParser()
{

}

std::vector<std::string> JsonFileParser::parserFile2Vec(const QString &fileName)
{
    std::vector<std::string> types;
    QSharedPointer<BaseFileParser> fileParser;
    // 读取json文件
     QString fileContent = readFile(fileName);
    if(fileContent == "None"){
        qDebug() << "read file error";
        return types;
    }
    // 解析json文件
    QJsonParseError jsonErr;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(fileContent.toUtf8(),&jsonErr);
    if(jsonErr.error!=QJsonParseError::NoError){
        qDebug() << "json error: "<<jsonErr.errorString();
        return types;
    }
    QJsonObject rootObj = jsonDoc.object(); // json文件根对象
    QJsonValue viewModelVal = rootObj.value("ViewModel");
    if(!viewModelVal.isArray()){
        qDebug() << "viewmodel is not jsonarray";
        return types;
    }
    QJsonArray viewModelArr = viewModelVal.toArray();
    for(int i=0;i<viewModelArr.size();++i){
        QJsonValue viewModel = viewModelArr.at(i);
        if(viewModel.isObject()){
            // 获取viewmodel的名称和id
            QString viewModelType = viewModel.toObject().value("ViewModelType").toString();
            int viewModelId = viewModel.toObject().value("ViewModelId").toInt();
            qDebug() << viewModelType <<" "<< viewModelId;
            types.emplace_back(viewModelType.toStdString());
        }else{
            qDebug() << "json file error";
            return types;
        }
    }
    return types;
}
